package com.example.testserviceaidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;

import java.util.Random;

public class MyService extends Service {

    private int mRandomSum = 0;

    @Override
    public void onCreate() {
        LogUtil.printLog("MyService onCreate");
        super.onCreate();
    }

    // Binder given to clients
    //private final IBinder binder = new LocalBinder();
    private final IRemoteService.Stub binder = new IRemoteService.Stub() {
        public int getPid(){
            return Process.myPid();
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
                               float aFloat, double aDouble, String aString) {
            // Does nothing
        }
        /** method for clients */
        public int getRandomNumber() {
            int randomnum = mGenerator.nextInt(100);
            int prenum = mRandomSum;
            mRandomSum += randomnum;
            LogUtil.printLog("MyService.getRandomNumber prenum: " + prenum + " randomnum:" + randomnum + " mRandomSum:" + mRandomSum);
            return randomnum;
        }
    };

    // Random number generator
    private final Random mGenerator = new Random();
    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */

    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.printLog("MyService.IBinder onBind");
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtil.printLog("MyService.IBinder onUnbind");
        return super.onUnbind(intent);
    }

}
